#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
	{
		int data;
		struct node *link;
	}*start =NULL;


void append()
	{
		int n;
		printf("\nEnter the number:");
		scanf("%d",&n);
		struct node *a;
		a=(struct node *)malloc(sizeof(struct node));
		a->data=n;
		a->link=start;
		start=a;
	}


void print()
	{
		struct node *pr;
		pr=start;
		printf("\nList is:\t");
		for(;pr!=NULL;pr=pr->link)printf("%d->",pr->data);
	}


void rev(){
		struct node *p1,*p2,*p3;
		int x;
		p1=start;
		printf("\nReversed");
		for(p2=start;p2->link!=NULL;p2=p2->link);
		while(p2!=p1 && p2->link!=p1)
			{
				x=p1->data;
				p1->data=p2->data;
				p2->data=x;
				p1=p1->link;
				p3=p2;
				for(p2=start;p2->link!=p3 && p2!=NULL;p2=p2->link);
			}print();}


void remov()
{	int num;
	printf("Enter number to delete:\t");
	scanf("%d",&num);
        struct node *node1=start;
	struct node *z;
        if(node1->data==num)				//removing first
	{
                start=start->link;
	        return;
        }
	for(;node1->link!=NULL;node1=node1->link)		//removing from middle
		{
			if(node1->link->data==num)
				node1->link=node1->link->link;
		}	
	while(node1->link==NULL)			//removing end
	{
		node1=node1->link;
	}
	z->link=node1;
	free(node1);
	z->link=NULL;
	return;						
}


void insertt()
	{
		int el,pos;
		int count=0;
		printf("Enter the element to be inserted : ");
		scanf("%d",&el);
		printf("Enter position : ");
		scanf("%d",&pos);
		struct node *p,*a;
		if(pos==0)
		{		
		struct node *temp;
		temp=(struct node*)malloc(sizeof(struct node));
		if(temp!=NULL)
		{
			temp->data=el;
			temp->link = start;
			start=temp;
		}
		}
		else{
		for(p=start;p->link!=NULL;p=p->link)
			count++;
	//check nodes in list
		if(pos <= (count+1))
		{
			p=start;
			while(pos-1)
			{
				p= p->link;
				pos--;
			}
			a=(struct node*)malloc(sizeof(struct node));	
		a->data=el;
		a->link=p->link;
		p->link=a;
	
		}
		else
		{
			printf("No new nodes can be added...Not enough nodes available");
		}
	}
	}


void extend()
{
	int n,i;
	printf("Enter the size of the list to be extended : ");
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		append();
	}
}


void pop()
{	
	struct node *temp=start;
	struct node *pre,*temp1;
	if(temp->link==NULL)
	{	
		start=0;
		free(temp);
		printf("\nThe stack is empty and cannot pop further..");
		return;
	}	
	else{
	while(temp->link!=0)
	{
		pre=temp;
		temp=temp->link;
	}
	printf("%d",pre->data);
	temp1=pre->link;
	pre->link=NULL;
	free(temp1);
	return;
	}
}


void inde()
	{	
		int k;
		struct node *node1;
		printf("Enter the index: ");
		scanf("%d",&k);
    	        node1=start;
	        int o=1;
	        for(;node1!=NULL;node1=node1->link,o++)
				if(node1->data==k)
	                        { 
					printf("\n%d Position!",o); return;
				}
                if(node1==NULL)
	        printf("\nNot Found!");
	}


void sort()
	{
		int x,y;
		struct node *p1,*q2;
		p1=start;
		for(;p1!=NULL;p1=p1->link)
			{
		        	y=p1->data;
	                	for(q2=start;q2!=NULL;q2=q2->link)
			                if(y<q2->data)
                                        {
        	                                 x=q2->data;
                                                 q2->data=p1->data;
                                                 p1->data=x;
					}
			}
		printf("\nSorted");
		print();
	}


void main()
{
	int num,n;
	struct node *head = NULL;
//	struct node *todelete;
	do
	{
printf("\n=====================================");
printf("\n=====================================");
printf("\n\n1.Appending The List");
printf("\n2.Reversing The List");
printf("\n3.Printing The List");
printf("\n4.Insert Node");
printf("\n5.Extending a List");
printf("\n6.Pop a Node");
printf("\n7.Index of a Node");
printf("\n8.Sorting The List");
printf("\n9.Deleting a Node");
printf("\n10.Exit");
printf("\n=====================================");
printf("\n\nEnter ur Choice : ");
scanf("%d",&num);

switch(num)
		{
		case 1:{append();}
			break;
		case 2:{rev();}
			break;
		case 3:{print();}
			break;
		case 4:{insertt();}
			break;
		case 5:{extend();}
			break;
		case 6:{pop();}
			break;
		case 7:{inde();}
			break;
		case 8:{sort();}
			break;
		case 9:{remov();}
			break;
		case 10:return;}
	}
while(1);
}
